Passed
Pull Request — master (#112)
by Mathieu
01:49
created

HolidayToEventsConverter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 28
dl 0
loc 32
c 0
b 0
f 0
rs 10

1 Function

Rating   Name   Duplication   Size   Complexity  
A convert 0 24 4
1
import {Inject} from '@nestjs/common';
2
import {
3
  Holiday,
4
  HolidayLeaveType
5
} from '../../HumanResource/Holiday/Holiday.entity';
6
import {Event, EventType} from 'src/Domain/FairCalendar/Event.entity';
7
import {IEventRepository} from '../Repository/IEventRepository';
8
import {IDateUtils} from 'src/Application/IDateUtils';
9
10
export class HolidayToEventsConverter {
11
  constructor(
12
    @Inject('IEventRepository')
13
    private readonly eventRepository: IEventRepository,
14
    @Inject('IDateUtils')
15
    private readonly dateUtils: IDateUtils
16
  ) {}
17
18
  public convert(holiday: Holiday): void {
19
    const user = holiday.getUser();
20
    const type =
21
      holiday.getLeaveType() === HolidayLeaveType.MEDICAL
22
        ? EventType.MEDICAL_LEAVE
23
        : EventType.HOLIDAY;
24
25
    const dates = this.dateUtils.getWorkedDaysDuringAPeriod(
26
      new Date(holiday.getStartDate()),
27
      new Date(holiday.getEndDate())
28
    );
29
30
    for (const date of dates) {
31
      const currentDate = date.toISOString();
32
      const firstDate = dates[0].toISOString();
33
      const lastDate = dates[dates.length - 1].toISOString();
34
      const time =
35
        (firstDate === currentDate && false === holiday.isStartsAllDay()) ||
36
        (lastDate === currentDate && false === holiday.isEndsAllDay())
37
          ? 50
38
          : 100;
39
40
      this.eventRepository.save(new Event(type, user, time, currentDate));
41
    }
42
  }
43
}
44